<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Support extends MY_Controller {

    public function __construct() {
      parent::__construct();      
            
      $this->load->model('users_mod');
      $this->load->model('messages_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('support_title');
        $data['keywords'] = lang('support_keywords');
        $data['description'] = lang('support_description');
        //$data['cities'] = $this->cities_mod->getCitiesList();
        $user_id = $this->session->userdata('user_id');
        $user = $this->users_mod->getUserData($user_id,false);
        $data['user'] = $user;
        $data['main_image'] = $this->images_mod->getMainImage($user_id);
        $data['support_is_online'] = $this->users_mod->isAdminOnline();
        $data['messages'] = $this->messages_mod->getSupportMessages();
        $data['menu_active'] = "support";
        $data['contents']  = "support/home";   
		$this->load->view('template/main',$data);
	}
    
    
    
    public function send_support_message()
    {
      try{
          
          //$to = $this->input->post('to');
          $to = 1; // salje se adminu as ID=1
          $message = $this->input->post('message');
          $from = $this->session->userdata('user_id');
          
          if( true == $this->messages_mod->sendSupportMessage($from,$message) )
          {
             $user_id = $this->session->userdata('user_id');
             $user = $this->users_mod->getUserData($user_id,false);
             $data['user'] = $user;
             $data['main_image'] = $this->images_mod->getMainImage($user_id);
             $data['support_is_online'] = $this->users_mod->isAdminOnline();
             $data['messages'] = $this->messages_mod->getSupportMessages();
             $html = $this->load->view('support/messages_list',$data,true);
            
            $response = array(
                'ind' => 1,
                'html' => $html
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('message_not_send')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
} // end class
/* End of file support.php */
/* Location: ./application/controllers/support.php */